
//
//  XHVideoFeedbackView.m
//  ShowYork
//
//  Created by hxh on 2020/9/4.
//  Copyright © 2020 hxh. All rights reserved.
//
typedef void(^doneBlock)(NSString *content);
#define CONTAINWIDTH 280
#import "XHVideoFeedbackView.h"
#import "WHToast.h"
@interface XHVideoFeedbackView ()<UITextViewDelegate>
@property (nonatomic,strong)UIView *containView;
@property (nonatomic,strong)UIView *titleView;
@property (nonatomic,strong)UIView *line;
@property (nonatomic,strong)UILabel *titleLb;
@property (nonatomic,strong)UILabel *otherTitleLb;
@property (nonatomic,strong)UIView *inputView;
@property (nonatomic,strong)UIButton *confirmBtn;
@property (nonatomic,strong)UITextView *textView;
@property (nonatomic,strong)UILabel *placeLb;
@property (nonatomic,strong)UIView *selectedView;

@property (nonatomic,copy)NSArray *titleArray;
@property (nonatomic,copy)doneBlock done;
@end

@implementation XHVideoFeedbackView

- (instancetype)initWithCompleteBlock:(void (^)(NSString * content))completeBlock{
    if (self = [super init]) {
        if (completeBlock) {
            self.done = ^(NSString *content) {
                completeBlock(content);
            };
        }
        [self setUI];
        [self registNote];
    }
    return self;
}

- (void)setUI{
    
    CGFloat maxW = kScreenW;
    CGFloat maxH = kScreenH;
    
    if ([UIApplication sharedApplication].statusBarOrientation == UIDeviceOrientationPortrait ||
    [UIApplication sharedApplication].statusBarOrientation == UIDeviceOrientationPortraitUpsideDown) {
        //当前竖屏
        maxW = kScreenH;
        maxH = kScreenW;
    }
    
    
    self.titleArray = @[@"板书有误",@"声音有误",@"字幕有误",@"太难了",@"听不懂",@"课程时间过长",@"其他"];
    self.frame = CGRectMake(0, 0, maxW, maxH);
    self.containView = [[UIView alloc]init];
    self.containView.backgroundColor = RGBA(0, 0, 0, 0.7);
    [self addSubview:self.containView];
    
    

    [self.containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self);
        make.width.mas_equalTo(CONTAINWIDTH);
        make.right.equalTo(self).offset(CONTAINWIDTH);
    }];
    
    self.titleView = [[UIView alloc]init];
    self.titleView.backgroundColor = [UIColor whiteColor];
    self.titleView.layer.cornerRadius = 2;
    self.titleView.layer.masksToBounds = YES;
    [self.containView addSubview:self.titleView];
    
    [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.containView).offset(13);
        make.size.mas_equalTo(CGSizeMake(4, 16));
        make.top.equalTo(self.containView).offset(11);
    }];
    
    self.titleLb = [[UILabel alloc]init];
    self.titleLb.text = @"提交您遇到的问题，我们会尽快处理";
    self.titleLb.textColor = [UIColor whiteColor];
    self.titleLb.font = font(12);
    [self.containView addSubview:self.titleLb];
    
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titleView);
        make.left.equalTo(self.titleView.mas_right).offset(12);
    }];
    
    self.line = [[UIView alloc]init];
    self.line.backgroundColor = [UIColor whiteColor];
    [self.containView addSubview:self.line];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLb.mas_bottom).offset(10);
        make.left.right.equalTo(self.containView);
        make.height.mas_equalTo(0.5);
    }];

    __block UIView *lastView = nil;
    
    CGFloat width = CONTAINWIDTH  * 0.5;
    CGFloat height = 35;
    [self.titleArray enumerateObjectsUsingBlock:^(NSString *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSInteger row = idx/2;
        NSInteger colum = idx%2;
        
        UIView *view = [[UIView alloc]init];
        [self.containView addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.containView).offset(colum * width);
            make.top.equalTo(self.line.mas_bottom).offset(row * height);
            make.size.mas_equalTo(CGSizeMake(width, height));
        }];
        
        UIView *coverView = [[UIView alloc]init];
        coverView.layer.cornerRadius = 5;
        coverView.layer.masksToBounds = YES;
        coverView.backgroundColor = idx == 0?[RPAppManager themColor]:[UIColor whiteColor];
        [view addSubview:coverView];
        
        [coverView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(view);
            make.size.mas_equalTo(CGSizeMake(10, 10));
            make.left.equalTo(view).offset(27);
        }];
        
        UILabel *lb = [[UILabel alloc]init];
        lb.text = obj;
        lb.textColor = [UIColor whiteColor];
        lb.font = font(14);
        [view addSubview:lb];
        [lb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(coverView.mas_right).offset(10);
            make.centerY.equalTo(coverView);
        }];
        
        
        if (idx == 0) {
            self.selectedView = view;
        }

        lastView = view;
        
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapMethod:)];
        [view addGestureRecognizer:tap];
    }];
    
    self.otherTitleLb = [[UILabel alloc]init];
    self.otherTitleLb.text = @"* 问题描述";
    self.otherTitleLb.textColor = [UIColor whiteColor];
    self.otherTitleLb.font = font(12);
    [self.containView addSubview:self.otherTitleLb];
    [self.otherTitleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lastView.mas_bottom).offset(16);
        make.left.equalTo(self.containView).offset(27);
    }];
    
    self.inputView = [[UIView alloc]init];
    self.inputView.backgroundColor = [UIColor whiteColor];
    self.inputView.layer.cornerRadius = 10;
    self.inputView.layer.masksToBounds = YES;
    [self.containView addSubview:self.inputView];
    [self.inputView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.containView).offset(27);
        make.right.equalTo(self.containView).offset(-27);
        make.top.equalTo(self.otherTitleLb.mas_bottom).offset(8);
        make.height.mas_equalTo(80);
    }];
    
    self.textView = [[UITextView alloc]init];
    self.textView.delegate = self;
    [self.containView addSubview:self.textView];
    
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.inputView);
    }];
    
    self.confirmBtn = [[UIButton alloc]init];
    [self.confirmBtn setTitle:@"提交" forState:UIControlStateNormal];
    self.confirmBtn.backgroundColor = [RPAppManager themColor];
    [self.confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.confirmBtn.titleLabel.font = font(14);
    self.confirmBtn.layer.cornerRadius = 17;
    self.confirmBtn.layer.masksToBounds = YES;
    [self.confirmBtn addTarget:self action:@selector(confirmMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.containView addSubview:self.confirmBtn];
        
    [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.inputView.mas_bottom).offset(20);
        make.size.mas_equalTo(CGSizeMake(120, 35));
        make.centerX.equalTo(self.containView);
    }];
    
    self.placeLb = [[UILabel alloc]init];
    self.placeLb.text = @"请输入问题描述，标注问题发生的时间进度，可以帮助我们更快更有效地定位问题哦。";
    self.placeLb.textColor = RGBA(204, 204, 204, 1);
    self.placeLb.font = font(12);
    self.placeLb.numberOfLines = 0;
    [self.containView addSubview:self.placeLb];
    [self.placeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.inputView).offset(7);
        make.top.equalTo(self.inputView).offset(8);
        make.right.equalTo(self.inputView).offset(-3);
    }];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)];
    [self addGestureRecognizer:tap];
}

- (void)show{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    [self layoutIfNeeded];
    
    [self.containView mas_updateConstraints:^(MASConstraintMaker *make) {
           make.right.equalTo(self);
    }];
    [UIView animateWithDuration:0.25 animations:^{
        [self layoutIfNeeded];
    }];
}
- (void)dismiss{
    
    BOOL stauts = [self.textView resignFirstResponder];
    if (!stauts) {
        [self removeView];
    }
}

- (void)tapMethod:(UIGestureRecognizer *)gestureRecognizer{
    UIView *currentView = gestureRecognizer.view;
    UIView *coverView = currentView.subviews.firstObject;
    if (![self.selectedView isEqual:currentView]) {
        UIView *lastView = self.selectedView.subviews.firstObject;
        lastView.backgroundColor = [UIColor whiteColor];
        coverView.backgroundColor = [RPAppManager themColor];
    }
    self.selectedView = currentView;
 

}

- (void)registNote{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

#pragma maark - 监听键盘时间
- (void)keyboardWillShow:(NSNotification *)notification
{
    CGFloat time = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey]floatValue];
    CGRect keyboardBounds = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];

    [self layoutIfNeeded];
      
    [self.containView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(-keyboardBounds.size.height + 60);
    }];
    [UIView animateWithDuration:time animations:^{
        [self layoutIfNeeded];
    }];

    
}
- (void)keyboardWillHide:(NSNotification *)notification
{
    CGFloat time = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey]floatValue];

    [self layoutIfNeeded];
      
    [self.containView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
    }];
    
    
    [UIView animateWithDuration:time animations:^{
        [self layoutIfNeeded];
        
    }];
}


- (void)removeView{
    [self.containView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(CONTAINWIDTH);
    }];
    [UIView animateWithDuration:0.25 animations:^{
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
            UILabel *lb = [self.selectedView.subviews lastObject];
            NSString *content = nil;
            if (self.textView.text.length > 0) {
                content = [NSString stringWithFormat:@"%@:%@",lb.text,self.textView.text];
            }
            if (self.done) {
                self.done(content);
            }
        }
    }];
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

- (void)confirmMethod{
    if (self.textView.text.length == 0 || [self.textView.text isSpaceString]) {
        [WHToast setPadding:15];
        [WHToast setFontSize:14];
        [WHToast showMessage:@"输入问题描述才能提交哦～" duration:2 finishHandler:nil];
    }else{
        [self dismiss];
    }

}

#pragma mark - UITextViewDelegate
-(void)textViewDidChange:(UITextView *)textView{
    self.placeLb.hidden = textView.text.length > 0;
    
    NSString *toBeString = textView.text;
 
    //获取高亮部分
    UITextRange *selectedRange = [textView markedTextRange];
    UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
 
    // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
    if (!position)
    {
        if (toBeString.length > 100){
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:100];
            if (rangeIndex.length == 1){
                textView.text = [toBeString substringToIndex:100];
            }else{
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0,100)];
                textView.text = [toBeString substringWithRange:rangeRange];
            }
        }
    }
}
@end
